<?php

namespace App\Controller;

use App\Model\UserLibrary;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\PutMapping;
use Hyperf\HttpServer\Annotation\DeleteMapping;
use App\AsyncQueue\LibraryAsyncQueue;
use Hyperf\Di\Annotation\Inject;
use League\Flysystem\Filesystem;
use App\Libs\BuildLibrary;
use Darabonba\OpenApi\Models\Config;
use Hyperf\Collection\Arr;

class UserLibraryController extends AbstractController
{
    /**
     * @var LibraryAsyncQueue
     */
    #[Inject]
    protected LibraryAsyncQueue $libraryAsyncQueue;

    /**
     * @var BuildLibrary
     */
    #[Inject]
    protected BuildLibrary $buildLibrary;
     /**
     * @var Filesystem
     */
    #[Inject]
    private Filesystem $filesystem;
    public function index()
    {
        $page_size = $this->request->input('page_size', 10);
        $name = $this->request->input('name', '');
        $tag = $this->request->input('tag', '');
        $category_id = $this->request->input('category_id', '');
        $model = new UserLibrary();
        if($name) $model = $model->where('name', 'like', "%{$name}%");
        if($tag) $model = $model->where('tag', 'like', "%{$tag}%");
        // if($category_id) $model->where('category_id', $category_id);

        $list = $model->where('user_id', $this->request->user->id)->orderByDesc('id')->paginate($page_size);
        // $row = $list->first();
        // $size = Oss::getObject($row->url);
        
        // $data= (json_decode($size, true));
        // $update = [
        //         'size'=> Arr::get($data, 'FileSize.value'),
        //         'ext'=> Arr::get($data, 'Format.value'),
        //         'width'=> Arr::get($data, 'ImageWidth.value'),
        //         'height'=> Arr::get($data, 'ImageHeight.value'),
        //     ];
        // var_dump(value: $update);
        // $this->materialAsyncQueue->push(params: $list->first()->toArray());
        return $this->success($list);
    }

    public function show($id)
    {
        $row = UserLibrary::with('product')->where('user_id', $this->request->user->id)->findOrFail($id);
        $this->buildLibrary->start($row->toArray());
        return $this->success($row);
    }

    public function store()
    {
        $data = $this->request->all();
        $data['user_id'] = $this->request->user->id;
        $row = UserLibrary::create($data);
        $this->libraryAsyncQueue->push(params: $row->toArray());
        return $this->success($row);
    }

    public function update($id)
    {
        $row = UserLibrary::where('user_id', $this->request->user->id)->findOrFail($id);
        $row->name = $this->request->input('name', '');
        $row->save();
        return $this->success($row);
    }

    public function destroy($id)
    {
        if($id == 0) {
            $ids = $this->request->input('ids', []);
            if (empty($ids)) {
                return $this->error('请选择要删除的素材');
            }
            $ret = UserLibrary::where('user_id', $this->request->user->id)->whereIn('id', $ids)->delete();
            return $this->success($ret);
        }
        $userCategory = UserLibrary::where('user_id', $this->request->user->id)->findOrFail($id);
        $ret = $userCategory->delete();
        return $this->success($ret);
    }
}
